import 'dart:math';

import 'package:flutter/material.dart';

class MyApp14 extends StatelessWidget {
  // 2. 设置默认值
  const MyApp14({
    super.key,
    this.title = '',
    this.content = '',
    this.tColor = Colors.orange,
    this.cColor = Colors.yellow,
  });

  // 1. 定义属性
  final String title;
  final String content;
  final Color tColor;
  final Color cColor;

  // 重写build 返回需要封装的组件即可
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.purple,
          title: Center(
            child: Text(
              '标题为:$title',
              style: TextStyle(
                  color: tColor, fontSize: 30, fontWeight: FontWeight.w900),
            ),
          ),
        ),
        // 1.2 主体部分
        body: const MyContainer(),
      ),
    );
  }
}

// 随机颜色
Color getRandomColor() {
  // 随机数
  Random random = Random();
  return Color.fromRGBO(
      random.nextInt(256), random.nextInt(256), random.nextInt(256), 1.0);
}

class MyContainer extends StatelessWidget {
  const MyContainer({super.key});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        // 类似于 GridView
        SliverGrid.builder(
            // GridView 的属性
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                mainAxisSpacing: 5,
                crossAxisSpacing: 5,
                childAspectRatio: 4 / 3),
            itemCount: 6, // 个数
            // 构建函数
            itemBuilder: (context, index) {
              return Container(
                color: getRandomColor(),
                alignment: Alignment.center,
                child: Text('黑马程序员$index'),
              );
            }),
        // 类似于 ListView
        SliverList.builder(
            itemCount: 5,
            itemBuilder: (context, index) {
              return Container(
                color: getRandomColor(),
                height: 200,
                alignment: Alignment.center,
                child: Text('黑马程序员$index'),
              );
            }),
        SliverGrid.builder(
            // GridView 的属性
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                mainAxisSpacing: 5,
                crossAxisSpacing: 5,
                childAspectRatio: 4 / 3),
            itemCount: 6, // 个数
            // 构建函数
            itemBuilder: (context, index) {
              return Container(
                color: getRandomColor(),
                alignment: Alignment.center,
                child: Text('黑马程序员$index'),
              );
            }),
      ],
    );
  }
}
